package com.llkj.qingnianguoxue.home;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.llkj.qingnianguoxue.R;
import com.llkj.qingnianguoxue.adapter.MasterAdapter;
import com.llkj.qingnianguoxue.base.BaseActivity;
import com.llkj.qingnianguoxue.base.ConstantUserInfo;
import com.llkj.qingnianguoxue.base.MyApplication;
import com.llkj.qingnianguoxue.bean.MasterDataBean;
import com.llkj.qingnianguoxue.bean.response.ResponseList;
import com.llkj.qingnianguoxue.http.HttpMethod;
import com.llkj.qingnianguoxue.util.DisplayUtil;
import com.llkj.qingnianguoxue.util.GsonUtil;
import com.llkj.qingnianguoxue.util.SpUtil;
import com.llkj.qingnianguoxue.util.ToastUtil;
import com.llkj.qingnianguoxue.util.helper.LoadDataHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * 大师
 */
public class MasterActivity extends BaseActivity implements BaseQuickAdapter.RequestLoadMoreListener,
        LoadDataHelper.LoadMoreListener, SwipeRefreshLayout.OnRefreshListener{
    private SwipeRefreshLayout refreshLayout;
    private RecyclerView lvActions;
    private MasterAdapter masterAdapter;
    private List<MasterDataBean.MasterBean> mMasterData;
    private int totalSize, mPage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_master);
        setTitle("范儿导师", true, TITLE_TYPE_IMG, R.drawable.icon_back, false, -1, -1);
        initStatusbar(TSATUSBAR_TYPE_BLACK_TEXT,rlTitle,R.color.colorFAF9F9);

        initView();
        initListener();

        onRefresh();
    }
    private void initView() {
        refreshLayout = (SwipeRefreshLayout) findViewById(R.id.refresh_layout);
        refreshLayout.setColorSchemeResources(R.color.blue3989ff);
        lvActions = (RecyclerView) findViewById(R.id.recyclerView);
        int padding = DisplayUtil.dip2px(this, 10);
        lvActions.setPadding(padding, 0, padding, padding);
        lvActions.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));

        mMasterData = new ArrayList<>();
        masterAdapter = new MasterAdapter(R.layout.item_master, mMasterData);
        lvActions.setAdapter(masterAdapter);
    }

    private void initListener() {
        refreshLayout.setOnRefreshListener(this);
        masterAdapter.setOnLoadMoreListener(this);
        lvActions.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                if( MyApplication.getInstance().sputil.getBoolean(ConstantUserInfo.ISLOGIN)) {
                    MasterDataBean.MasterBean masterBean = mMasterData.get(i);
                    Bundle bundle = new Bundle();
                    bundle.putString("id", masterBean.getId());
                    startActivity(MasterDetailActivity.class, bundle);
                }else{
                    ToastUtil.makeShortText(MasterActivity.this,"请登录账号后才能观看");
                }
            }
        });

    }

    @Override
    public void onLoadMore() {
        mPage++;
        getMasters();
    }

    @Override
    public void onRefresh() {
        mPage = 1;
        getMasters();
    }

    @Override
    public void onLoadMoreRequested() {
        LoadDataHelper.loadMoreRequest(this, masterAdapter, null, lvActions, totalSize);
    }

    private void getMasters() {
        HttpMethod.artistIndex(this, this, String.valueOf(mPage));
    }

    @Override
    protected void onhttpSuccess(String data, int method) {
        super.onhttpSuccess(data, method);
        if (method == HttpMethod.HTTP_ARTIST_INDEX) {
            ResponseList<MasterDataBean.MasterBean> bean = GsonUtil.fromJsonList(data,MasterDataBean.MasterBean.class);
//            MasterDataBean hotRankingBean = GsonUtil.GsonToBean(data, MasterDataBean.class);
            if (bean != null) {
                totalSize = Integer.parseInt(bean.getNum());
                if (mPage == 1) {
                    mMasterData.clear();
                    LoadDataHelper.refreshComplete(masterAdapter, mMasterData, refreshLayout, lvActions);
                } else {
                    masterAdapter.setEnableLoadMore(true);
                    masterAdapter.loadMoreComplete();
                }
                mMasterData.addAll(bean.getArtist());
                masterAdapter.notifyDataSetChanged();
            }
        }
    }
}
